package com.luoqifei.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.*;

@SuppressWarnings("deprecation")
public class HBaseTestCase {

    static HBaseConfiguration cfg = null;
    static {
        Configuration HBASE_CONFIG = new Configuration();
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.1.10.38");
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
        cfg = new HBaseConfiguration(HBASE_CONFIG);
    }

    /**
     * 创建一张表
     */
    public static void creatTable(String tablename) throws Exception {
        HBaseAdmin admin = new HBaseAdmin(cfg);
        if (admin.tableExists(tablename)) {
            System.out.println("table   Exists!!!");
        }
        else{
            HTableDescriptor tableDesc = new HTableDescriptor(tablename);
            tableDesc.addFamily(new HColumnDescriptor("cf1"));
            admin.createTable(tableDesc);
            System.out.println("create table ok .");
        }

    }

    /**
     * 添加一条数据
     */
    public static void addData (String tablename) throws Exception{
        HTable table = new HTable(cfg, tablename);
        //add data to row4
        Put put = new Put(new String("row4").getBytes());
        //this data in cf column family,and in c column, the value is value3
        put.add(new String("cf1").getBytes(),new String("c").getBytes(),new String("valus3").getBytes());
        table.put(put);
        table.flushCommits();
        System.out.println("add data ok .");
    }

    /**
     * 显示所有数据
     */
    public static void getAllData (String tablename) throws Exception{
        HTable table = new HTable(cfg, tablename);
        Scan s = new Scan();
        ResultScanner ss = table.getScanner(s);
        for(Result r:ss){
            for(KeyValue kv:r.raw()){
                System.out.print(new String(kv.getFamily()));
                System.out.println(new String(kv.getValue()));
            }

        }
    }


    public static void  main (String [] agrs) {
        try {
            String tablename="test2";
            HBaseTestCase.creatTable(tablename);
            HBaseTestCase.addData(tablename);
            HBaseTestCase.getAllData(tablename);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

}